package ui.pfont;

import java.io.File;
import java.io.FileInputStream;
import java.util.Arrays;


public class PFontLibrary {

    public static void init(boolean force) {
        if (init == false || force) {
            fontsizescount = new int[MAX_PCF_FAMILY];
            fontsizes = new int[MAX_PCF_FAMILY][MAX_PCF_SIZE];
            fonts = new PFont[MAX_PCF_FAMILY][MAX_PCF_SIZE];

            for (int i = 0; i < MAX_PCF_FAMILY; i++) {
                fontsizescount[i] = 0;
                for (int j = 0; j < MAX_PCF_SIZE; j++) {
                    fonts[i][j] = null;
                    fontsizes[i][j] = -1;
                }
            }
            init = true;
        }
    }

    public static void load() {
        init(false);
        
        /*
         * TODO: investigate why MISCFIXED fonts take so long to load.
         */

        loadFamily(TERMINUS,      TERMINUS_PATH);
        
        loadFamily(TERMINUS_BOLD, TERMINUS_BOLD_PATH);

        loadFamily(MISCFIXED, MISCFIXED_PATH);
        
        // ... and so on
    }

    protected static boolean loadFamily(int family, String path) {
        int i;
        int size;
        File file;
        PFont font;
        File dir = new File(path);

        File[] children = dir.listFiles();
        if (children == null) {
            return false;
        } else {
            for (i = 0; i < children.length; i++) {
                file = children[i];
                try {
                    font = new PFont(new FileInputStream(file));
                    if (font != null) {
                        /*
                         * Insert the font int the fonts db
                         */ 
                        size = font.getHeight();
                        fonts[family][size] = font;

                        /*
                         * Update the font size db
                         */ 
                        fontsizes[family][fontsizescount[family]++] = size;
                    }
                } catch (Exception e) {
                    continue;
                }
            }

            /*
             * Sort the font size db for this family
             */ 
            Arrays.sort(fontsizes[family], 0, fontsizescount[family]);
        }

        return true;
    }

    public static PFont getFont(int family, int sizeidx) {
        if (family >= MAX_PCF_FAMILY || sizeidx >= fontsizescount[family]) {
            return null;
        }

        return fonts[family][fontsizes[family][sizeidx]];
    }

    protected static int MAX_PCF_FAMILY = 10;
    protected static int MAX_PCF_SIZE = 30;
    protected static PFont fonts[][];
    protected static int fontsizes[][];
    protected static int fontsizescount[];
    protected static boolean init = false;

    /*
     * Fonts and their directories
     */ 
    public static int TERMINUS      = 1;
    public static int TERMINUS_BOLD = 2;
    public static int MISCFIXED     = 3;
    
    public static String TERMINUS_PATH = "lib/ui/pfont/pcffont/terminus";
    public static String TERMINUS_BOLD_PATH = "nopath";
    public static String MISCFIXED_PATH = "lib/ui/pfont/pcffont/miscfixed";

}
